from rest_framework import serializers
from frame_server.serializers import BaseSerializer as BSerializer
from .models import Dept

class BaseSerializer(BSerializer):
    id = serializers.CharField()
    hasChildren = serializers.SerializerMethodField()
    children = serializers.SerializerMethodField()
    parentId = serializers.CharField(source="parentId_id", required=False, allow_null=True)
    parentName = serializers.CharField(source="parentId.deptName", required=False)

    class Meta:
        model = Dept
    
    def get_hasChildren(self,obj):
        return obj.dept_set.exists()
    
    def get_children(self,obj):
        # 动态获取当前序列化器类
        serializer_class = self.__class__
        objs = obj.dept_set
        if objs.exists():
            return serializer_class(objs, many=True).data
        return []

# 菜单管理-部门表格数据序列化
class LazyListSerializer(BaseSerializer):
    class Meta(BaseSerializer.Meta):
        fields = '__all__'

# 部门完整树结构
class TreeSerializer(BaseSerializer):
    class Meta(BaseSerializer.Meta):
        fields = '__all__'

# 部门详情
class DetailSerializer(BaseSerializer):
    class Meta(BaseSerializer.Meta):
        fields = "__all__"

